Skip to contentMethod: randomPositiveInt(int, int)
1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.environment;
16:
17: import cz.cvut.kbss.jopa.test.OWLClassA;
18:
19: import java.math.BigDecimal;
20: import java.math.BigInteger;
21: import java.net.MalformedURLException;
22: import java.net.URI;
23: import java.net.URL;
24: import java.time.LocalDate;
25: import java.time.OffsetDateTime;
26: import java.time.OffsetTime;
27: import java.time.temporal.ChronoUnit;
28: import java.util.*;
29: import java.util.stream.Collectors;
30:
31: /**
32: * Generators of test data.
33: */
34: public abstract class Generators {
35:
36: private static final int DEFAULT_MIN = 5;
37: private static final int DEFAULT_SIZE = 10;
38: private static final Set<String> TYPES = getTypes();
39:
40: private static final String PROPERTY_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes#property";
41: private static final String TYPE_URI_BASE = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#Entity";
42:
43: private static final Random RANDOM = new Random();
44:
45: private Generators() {
46: // Private constructor
47: }
48:
49: public static List<OWLClassA> createSimpleList() {
50: return createSimpleList(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
51: }
52:
53: public static List<OWLClassA> createSimpleList(int size) {
54: assert size > 0;
55: final List<OWLClassA> lst = new ArrayList<>(size);
56: generateInstances(lst, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimple",
57: size);
58: return lst;
59: }
60:
61: public static List<OWLClassA> createReferencedList() {
62: return createReferencedList(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
63: }
64:
65: public static List<OWLClassA> createReferencedList(int size) {
66: assert size > 0;
67: final List<OWLClassA> lst = new ArrayList<>(size);
68: generateInstances(lst,
69: "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityAReferenced", size);
70: return lst;
71: }
72:
73: public static List<URI> createListOfIdentifiers() {
74: return createSimpleList().stream().map(OWLClassA::getUri).collect(Collectors.toList());
75: }
76:
77: public static Set<OWLClassA> createSimpleSet() {
78: return createSimpleSet(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
79: }
80:
81: public static Set<OWLClassA> createSimpleSet(int size) {
82: assert size > 0;
83: final Set<OWLClassA> set = new HashSet<>(size);
84: generateInstances(set, "http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityASimpleSet",
85: size);
86: return set;
87: }
88:
89: public static Map<String, Set<String>> createProperties() {
90: return createProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
91: }
92:
93: public static Map<String, Set<String>> createProperties(int size) {
94: assert size > 0;
95: final Map<String, Set<String>> m = new HashMap<>(size);
96: int counter = randomInt(1000);
97: for (int i = 0; i < size; i++) {
98: final Set<String> value = new HashSet<>(4);
99: for (int j = 0; j < size; j++) {
100: value.add("http://krizik.felk.cvut.cz/ontologies/jopa/tests/ObjectPropertyValue_" + j + "_"
101: + counter);
102: }
103: m.put(PROPERTY_URI_BASE + counter, value);
104: counter++;
105:
106: }
107: return m;
108: }
109:
110: public static Map<URI, Set<Object>> createTypedProperties() {
111: return createTypedProperties(randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE));
112: }
113:
114: public static Map<URI, Set<Object>> createTypedProperties(int size) {
115: assert size > 0;
116: final Map<URI, Set<Object>> props = new HashMap<>(size);
117: int counter = randomInt(1000);
118: for (int i = 0; i < size; i++) {
119: final Set<Object> value = new HashSet<>();
120: for (int j = 0; j < size; j++) {
121: // Generate either an individual's URI or random data value. But same type for a property
122: // (so that the property is either object or data, but not both)
123: if (counter % 2 == 0) {
124: value.add(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/Property_" + counter +
125: "Individual_" + j));
126: } else {
127: value.add(generateRandomPropertyValue(j, counter));
128: }
129: }
130: props.put(URI.create(PROPERTY_URI_BASE + counter), value);
131: counter++;
132: }
133: return props;
134: }
135:
136: private static Object generateRandomPropertyValue(int valueIndex, int propertyIndex) {
137: final int random = randomInt(10);
138: switch (random) {
139: case 0: // boolean
140: return valueIndex % 2 == 0;
141: case 1: // int
142: return valueIndex;
143: case 2: // long
144: return System.currentTimeMillis();
145: case 3: //double
146: return ((double) propertyIndex + 1) / (valueIndex + 1);
147: case 4: // datetime
148: // Generate date rounded to milliseconds to prevent issues with time rounding
149: return OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS);
150: case 5:
151: return OffsetTime.now().truncatedTo(ChronoUnit.MILLIS);
152: case 6:
153: return LocalDate.now();
154: case 7: // String
155: return "TypedProperty_" + propertyIndex + "Value_" + valueIndex;
156: case 8:
157: return BigInteger.valueOf(valueIndex);
158: case 9:
159: return BigDecimal.valueOf(Math.PI);
160: default:
161: throw new IllegalArgumentException();
162: }
163: }
164:
165: private static void generateInstances(Collection<OWLClassA> col, String uriBase, int size) {
166: assert size > 0;
167: int counter = randomInt(1000);
168: for (int i = 0; i < size; i++) {
169: final OWLClassA a = new OWLClassA();
170: a.setUri(URI.create(uriBase + counter));
171: a.setStringAttribute("stringAttributeeee" + counter);
172: counter++;
173: a.setTypes(TYPES);
174: col.add(a);
175: }
176: }
177:
178: private static Set<String> getTypes() {
179: final Set<String> types = new HashSet<>(3);
180: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassDF");
181: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassDFF");
182: types.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassDFFF");
183: return types;
184: }
185:
186: public static Set<URL> createUrls() {
187: return Generators.createSimpleList().stream().map(a -> {
188: try {
189: return a.getUri().toURL();
190: } catch (MalformedURLException e) {
191: throw new IllegalArgumentException(e);
192: }
193: }).collect(Collectors.toSet());
194: }
195:
196: public static int randomInt() {
197: return RANDOM.nextInt();
198: }
199:
200: public static int randomInt(int max) {
201: return RANDOM.nextInt(max);
202: }
203:
204: /**
205: * Gets a random int between {@code min} and {@code max}.
206: *
207: *
208: * @param min lower bound (inclusive)
209: * @param max upper bound (exclusive)
210: * @return Random positive integer
211: */
212: public static int randomPositiveInt(int min, int max) {
213:• assert min >= 0;
214:• if (max <= min) {
215: throw new IllegalArgumentException("Upper bound has to be greater than the lower bound.");
216: }
217: int rand;
218:• do {
219: rand = RANDOM.nextInt(max);
220: } while (rand < min);
221: return rand;
222: }
223:
224: public static boolean randomBoolean() {
225: return RANDOM.nextBoolean();
226: }
227:
228: public static Set<URI> createUriTypes() {
229: final int count = randomPositiveInt(DEFAULT_MIN, DEFAULT_SIZE);
230: final Set<URI> result = new HashSet<>();
231: for (int i = 0; i < count; i++) {
232: result.add(URI.create(TYPE_URI_BASE + randomInt(Integer.MAX_VALUE)));
233: }
234: return result;
235: }
236:
237: /**
238: * Generates a random URI.
239: *
240: * @return Random URI
241: */
242: public static URI generateUri() {
243: return URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/individuals#instance" +
244: randomInt(Integer.MAX_VALUE));
245: }
246:
247: /**
248: * Gets random item from the specified list.
249: * @param items List of items
250: * @return Random item from the list
251: */
252: public static <T> T getRandomItem(List<T> items) {
253: return items.get(randomPositiveInt(0, items.size()));
254: }
255:
256: public static Random getRandomGenerator() {
257: return RANDOM;
258: }
259: }